const { ipcRenderer } = require('electron');
const { getCurrentWindow, dialog, shell, app } = require('@electron/remote');

(window as any).api = {
  operateWindow: (type) => {
    ipcRenderer.send('operateWindow', type);
  },
  isMaximze: () => {
    return getCurrentWindow().isMaximized();
  },
  getScreenSource: (callback) => {
    ipcRenderer.send('getScreenSource');
    ipcRenderer.on('SET_SOURCE', (event, windows) => {
      callback(windows);
    });
  },
  downloadFile: (data, dir) => {
    console.log(data);
    ipcRenderer.send('downloadFile', {
      data,
      cacheDir: dir
    });
  },
  getCacheVideo: (dir) => {
    return new Promise((resolve) => {
      ipcRenderer.send('getCacheVideo', { cacheDir: dir });
      ipcRenderer.once('getCacheSuccess', (e, data) => {
        resolve(data || []);
      });
    });
  },
  createRecorderWindow: () => {
    ipcRenderer.send('createRecorderWindow');
  },
  // 打开mac开关
  openMacSwitch: () => {
    ipcRenderer.send('openMacSwitch');
  },
  openDirectory: async (params = {}) => {
    try {
      const data = await dialog.showOpenDialog({ ...params });
      console.log('打开文件', data);
      return data;
    } catch (error) {}
  },
  readyRecord: () => {
    ipcRenderer.send('readyRecord');
  },
  onShotCut: (callback) => {
    ipcRenderer.on('operateRecorder', (e, data) => {
      callback?.(data);
    });
  },
  broadcast: (msg) => {
    ipcRenderer.send('broadcast', msg);
  },
  receptBroadcast: (callback) => {
    ipcRenderer.on('broadcast', (e, data) => {
      callback(data);
    });
  },
  openSystemSource: (path) => {
    shell.showItemInFolder(path);
  },
  playerVideo: (path) => {
    ipcRenderer.send('playerVideo', path);
  },
  formatVideo: (data, callback) => {
    ipcRenderer.send('formatVideo', data);
    ipcRenderer.on('formatVideoProgress', (e, res) => {
      callback(res);
    });
  },
  getVideoCacheDir: () => {
    return `${app.getPath('appData')}/${app.getName()}/cache`;
  },
  sendMsg: (data) => {
    ipcRenderer.send('webMsg', data);
  }
};
export {};
